using GLib;


namespace Malem {
	
	protected class ServiceData : GLib.Object {
		private Gee.ArrayList<JobObject> jobs;
		
		public string commandName {get;construct;}
		public string uniqueCommandName {get;construct;}
		public string dbusPath {get; construct;}
		public string dbusName {get; construct;}
		public bool autoload {get; construct;}
		private dynamic DBus.Object obj;
		
		public ServiceData(string commandName, string uniqueCommandName, string DBusName, string DBusPath, bool autoload) {
			this.commandName = commandName;
			this.uniqueCommandName = uniqueCommandName;
			this.dbusPath = DBusPath;
			this.dbusName = DBusName;
			this.autoload = autoload;
		}
		
		construct {
			this.jobs = new Gee.ArrayList<JobObject>();
		}
		
		public void setDBusObject(dynamic DBus.Object service) {
			this.obj = service;
		}

		public void addJob(JobObject jo) {
			this.jobs.add(jo);
		}
		
		~ServiceData() {
			this.jobs.clear();
		}
		
		private Gee.ArrayList<string> params;
		
		public dynamic DBus.Object getDBusObject() {
//			if (this.obj==null)
//				this.obj = conn.get_object(this.dbusName, this.dbusPath, "org.gnome.Mathusalem.Service");

			return this.obj;
		}
		
		public Gee.ArrayList<string> getJobsParameters() {
			if (params==null) {
				string[] p;
				try {
					p = obj.getJobsParameters();
				} catch (Error e) {
					p = new string[0];
				}
				params = new Gee.ArrayList<string>();
				foreach(string s in p)
					params.add(s);
				malem.Util.sortArrayListByString(this.params, 0, this.params.size-1);
			}
			return params;
		}
		
		public Gee.ArrayList<string> getJobSupportedInterface(Gee.HashMap<string, string> init_parameters) {
			Gee.ArrayList<string> interfaces;
			string[] p;
			
			HashTable<string, string> ht = new HashTable<string, string>.full(str_hash, str_equal, g_free, g_free);
			foreach(string s in init_parameters.get_keys()) {
				ht.insert(s, init_parameters.get(s));
			}
			try {
				p = obj.getJobSupportedInterfaces(ht);
			} catch (Error e) {
				p = new string[0];
			}
			interfaces = new Gee.ArrayList<string>();
			foreach(string s in p)
				interfaces.add(s);
			malem.Util.sortArrayListByString(interfaces, 0, interfaces.size-1);
			return interfaces;
		}

		
		public bool canDoThisJob(Gee.Map<string, string> init_parameters, out string optional_message) {
			HashTable<string, string> ht = new HashTable<string, string>.full(str_hash, str_equal, g_free, g_free);
			foreach(string s in init_parameters.get_keys()) {
				ht.insert(s, init_parameters.get(s));
			}
			return obj.canDoThisJob(ht, out optional_message);
		}
		
		public bool startJob(Gee.HashMap<string, string> init_parameters, out string dbus_name, out string dbus_path) {
			HashTable<string, string> ht = new HashTable<string, string>.full(str_hash, str_equal, g_free, g_free);
			foreach(string s in init_parameters.get_keys()) {
				ht.insert(s, init_parameters.get(s));
			}
			dbus_name = this.dbusName;
			return obj.startJob(ht, out dbus_path);
		}
	}

}
